package poker.beans;

import java.util.Vector;

import poker.UI;
import poker.beans.GameBean;

public class PlayerBean implements java.io.Serializable {
 
	private final String[] names = {"Batman","Alfred","Joker","Robin","Mr. Freeze","Riddler","Egghead","Penguin","Falce-Face"};
	private int id;	
    private String name; 
    private Action action;        
    private int money;    
    private boolean inGame;    
    private int strongestHand;    
    private int betSize;    
    private boolean isHuman;    
    private boolean hasActed;
    private Vector<CardBean> cards;
 
    public enum Action {FOLD, RAISE, CHECK, CALL, SB, BB, NOACTION};
        
    public PlayerBean() {
    }

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setAction(Action action) {
		this.action = action;
	}

	public Action getAction() {
		return action;
	}

	public void setMoney(int money) {
		this.money = money;
	}

	public int getMoney() {
		return money;
	}

	public void setInGame(boolean inGame) {
		this.inGame = inGame;
	}

	public boolean isInGame() {
		return inGame;
	}

	public void setStrongestHand(int strongestHand) {
		this.strongestHand = strongestHand;
	}

	public int getStrongestHand() {
		return strongestHand;
	}

	public void setBetSize(int betSize) {
		this.betSize = betSize;
	}

	public int getBetSize() {
		return betSize;
	}

	public void setHuman(boolean isHuman) {
		this.isHuman = isHuman;
	}

	public boolean isHuman() {
		return isHuman;
	}

	public void setHasActed(boolean hasActed) {
		this.hasActed = hasActed;
	}

	public boolean hasActed() {
		return hasActed;
	}
	
	public void addWinnings(int winnings){
		this.money += winnings;
	}
	
	public void increaseBet(int bet){
		this.betSize += bet;
		this.money -= bet;
	}
	
	public void initPlayer(int i) {
		id = i;
		betSize = 0;
		cards = new Vector<CardBean>(2);
		hasActed = false;
		inGame = true;
		money = 100;
		name = names[i];
		strongestHand = 0;
		action = Action.NOACTION;
		if(i == 0) isHuman = true;
		else isHuman = false;
		UI.updatePlayerData(this);
	}

	public void setCards(Vector<CardBean> cards) {
		this.cards = cards;
	}

	public Vector<CardBean> getCards() {
		return cards;
	}
	
	public CardBean getCard(int i){
		return cards.get(i);
	}
	
	public void addCard(CardBean card){
		cards.add(card);
	}
	
	public void makeAction( Action action, GameBean game ) {
				
		switch (action) {
        case FOLD:
        		fold(game);        		
                break;
        case CHECK:
                break;
        case CALL:
        		call(game);
                break;
        case RAISE:
        		raise(game);
                break;
        default:
                break;
		}
		hasActed = true;
		this.action = action;
		UI.removeActivePlayer( this );
	}
	
	public void fold(GameBean game) {
		inGame = false;
		cards.clear();
		game.decrementPlayersInGame();
		UI.foldPlayerCards(this);
	}
	
	
	public void call( GameBean game ) {
		this.bet( game, game.getMaxBet() - betSize );
	}
	
	public void raise( GameBean game ) {
		this.bet( game, game.getMaxBet() - betSize + game.getSmallBlind() * 2 * game.getBetMultiplier() );
		game.incrementTimesRaised();
	}
	
	public void bet( GameBean game, int amount ) {
		money -= amount;
		betSize += amount;
		if (game.getMaxBet() < betSize) game.setMaxBet(betSize);
		game.increasePot( amount );
	}

	public Action generateAction(GameBean game) {
		Action a;
		double x = Math.random();
		int moneyToCall = game.getMaxBet() - betSize;
		int raiseAmount = game.getSmallBlind() * 2 * game.getBetMultiplier();
				
		if( game.getMaxBet() == betSize 
				&& x < 0.3 ) a = Action.CHECK;
		
		else if( x < 0.7 
				&& money >= moneyToCall 
				&& moneyToCall > 0 
				&& game.getMaxBet() != 0) a = Action.CALL;
		
		else if ( money >= moneyToCall + raiseAmount 
				&&	game.getTimesRaised() < game.getBetLimit())	a = Action.RAISE;			
		
		else if( game.getMaxBet() == betSize ) a = Action.CHECK;
		
		else a = Action.FOLD; // TODO allIn();
		
		return a;
	}
}